<?php

namespace App\Http\Middleware;
use App\Dto\ClientDto;
use App\Enums\JwtUserTypeEnum;
use App\Exceptions\ErrorCode;
use App\Models\Client\ClientUserModel;
use Illuminate\Auth\AuthenticationException;

class ClientJwtAuthenticate extends \Tymon\JWTAuth\Http\Middleware\Authenticate
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return null|string
     */
    public function handle($request, \Closure $next)
    {
        $user = auth(JwtUserTypeEnum::CLIENT_USER)->user();
        if (blank($user)) {
            throw new AuthenticationException();
        }

        if ($user->status == ClientUserModel::STATUS_FORBIDDEN) {
            throw new AuthenticationException('禁止访问!',401);
        }
        //初始化dto
        ClientDto::setUser($user);
//        ClientDto::setWechatUser($user->wechatUser);
//        ClientDto::setSiteInfo($user->site);
//        ClientDto::setSiteId($user->site_id);
        ClientDto::setPlatform();
        //设置C端用户当前使用环境 mini:小程序端 web:pc端
        ClientDto::setClientChannel(JwtUserTypeEnum::CLIENT_USER);
        // 验证token有效性
        return $next($request);
    }
}
